/**
 * Copyright (c) 2015-2016, Javen Zhou  (javenlife@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.aef.controller;

import com.aef.common.menu.Button;
import com.aef.common.menu.ClickButton;
import com.aef.common.menu.ComButton;
import com.aef.common.menu.Menu;
import com.aef.common.menu.ViewButton;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.CallbackIpApi;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;

public class WeixinApiController extends ApiController {
	
	/**
	 * 如果要支持多公众账号，只需要在此返回各个公众号对应的  ApiConfig 对象即可
	 * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
	 */
	public ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();
		
		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setAppId(PropKit.get("appId"));
		ac.setAppSecret(PropKit.get("appSecret"));
		
		/**
		 *  是否对消息进行加密，对应于微信平台的消息加解密方式：
		 *  1：true进行加密且必须配置 encodingAesKey
		 *  2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));
		return ac;
	}

	/**
	 * 获取公众号菜单
	 */
	public void getMenu() {
		ApiResult apiResult = MenuApi.getMenu();
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}

	/**
	 * 创建菜单
	 */
	public void createMenu()
	{
		String domain=PropKit.get("domain");
		ClickButton clickButton1=new ClickButton();
		clickButton1.setName("关于我们");
		clickButton1.setType("click");
		clickButton1.setKey("Professional");
		ComButton comButton2=new ComButton();
		comButton2.setName("途狗收油");
		ViewButton btn210 = new ViewButton();
		btn210.setName("发起收油");
		btn210.setUrl(domain+"recycle/index");
		btn210.setType("view");
		ViewButton btn211 = new ViewButton();
		btn211.setName("个人中心");
		btn211.setUrl(domain+"recycle/index1");
		/*btn211.setUrl(domain+"weixinuser/newhtml/user_infoList.jsp");*/
		btn211.setType("view");
		comButton2.setSub_button(new Button[] { btn210,btn211});

		ComButton comButton3=new ComButton();
		comButton3.setName("收油商");
		ViewButton btn31 = new ViewButton();
		btn31.setName("收油业务");
		btn31.setUrl(domain+"salesman/index");
		btn31.setType("view");
		/*ClickButton btn32=new ClickButton();
		btn32.setName("行业信息");
		btn32.setType("click");
		btn32.setKey("Professional");*/
		comButton3.setSub_button(new Button[] { btn31 });
		Menu menu = new Menu();
		menu.setButton(new Button[] {clickButton1,comButton2,comButton3 });
		ApiResult apiResult = MenuApi.createMenu(JsonKit.toJson(menu).toString());
		
		//ApiResult apiResult = MenuApi.deleteMenu();
		System.out.println(apiResult);
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}

	/**
	 * 获取公众号关注用户
	 */
	public void getMyFollowers()
	{
		ApiResult apiResult = UserApi.getFollows();
		JSONObject jsonObject= JSONObject.parseObject(apiResult.getJson());
		JSONArray jsonArray=jsonObject.getJSONObject("data").getJSONArray("openid");
		JSONArray result =new JSONArray();
		for(int i=0;i<jsonArray.size();i++){
			result.add(JSONObject.parseObject(UserApi.getUserInfo((String)jsonArray.get(i)).getJson()));
		}
		renderJson(result);
	}
	/**
     * 获取用户信息
	 * @return 
     */
    public JSONObject getUserInfo(String openid)
    {
        ApiResult apiResult = UserApi.getUserInfo(openid);
        JSONObject jsonObject= JSONObject.parseObject(apiResult.getJson());
        /*System.out.println(apiResult.getJson());*/
		return jsonObject;
    }


	/**
	 * 获取客服信息
	 */
	public void getKF()
	{
		ApiResult apiResult = CustomServiceApi.getKfList();
		JSONObject jsonObject= JSONObject.parseObject(apiResult.getJson());
		renderJson(jsonObject);
	}
	
	/**
	 * 获取微信服务器IP地址
	 */
	public void getCallbackIp()
	{
		ApiResult apiResult = CallbackIpApi.getCallbackIp();
		renderText(apiResult.getJson());
	}
}

